<?php

class Communities extends HTech_Db_Table
{
    protected $_name    = 'view_communities';
    protected $_primary = 'CommunityId';

    protected function _setGridMetadata()
    {
        $this->_gridMetadata['title'] = $this->translate("Communities");
        $this->_gridMetadata['titleMods'] = array('LocationShortName','OfficerUsername');
        $this->_gridMetadata['params'] = array('LocationId');
        $this->_gridMetadata['onClick'] = 'admin.community.add';
        $this->_gridMetadata['headers'] = array
            (
                array('CommunityId',       50, $this->translate("Id"),             'Number'),
                array('Community',        300, $this->translate("Community"),      'String'),
                array('LocationName',     200, $this->translate("Location"),       'String'),
                array('Type',             100, $this->translate("Type"),           'String'),
                array('Latitude',         100, $this->translate("Latitude"),       'String'),
                array('Longitude',        100, $this->translate("Longitude"),      'String'),
                array('Comments',         500, $this->translate("Comments"),       'String')
            );
        return $this;
    }
        
    static function getCommunityOptions($type = '', $id = 0)
    {
        if(!$id) return array();
        switch($type) {
            case 'Location': 
                return Zend_Registry::get('db')->fetchPairs("select CommunityId, Community from communities 
                    where LocationId = ? order by Community", $id);
            case 'Officer':
                return Zend_Registry::get('db')->fetchPairs("select c.CommunityId, c.Community from communities c, view_officers o
                    where c.LocationId = o.LocationId and o.OfficerUserId = ? order by c.Community", $id);  
            case 'Bank' :
                return Zend_Registry::get('db')->fetchPairs("select c.CommunityId, c.Community from communities c, view_banks b 
                    where c.LocationId = b.LocationId and b.BankId = ? order by c.Community", $id); 
            case 'Group' :
                return Zend_Registry::get('db')->fetchPairs("select CommunityId, Community from view_groups 
                    where GroupId = ?", $id);
            case 'Client':
                require_once 'Clients.php';            
                    return Zend_Registry::get('db')->fetchPairs("select cm.CommunityId, cm.Community, cm.LocationId from communities cm, view_clients cl 
                        where case when (cl.GroupId = 0) then 1 else if(cm.CommunityId = cl.CommunityId, 1, 0) end 
                        and cm.LocationId = cl.LocationId and cl.ClientId = ?", $id);
                    /*
                if(Clients::getActiveLoansCount($id)) {
                    return Zend_Registry::get('db')->fetchPairs("select cm.CommunityId, cm.Community from communities cm, view_clients cl 
                        where cm.CommunityId = cl.CommunityId and cl.ClientId = ?", $id);
                } else {            
                    return Zend_Registry::get('db')->fetchPairs("select cm.CommunityId, cm.Community, cm.LocationId from communities cm, view_clients cl 
                        where case when (cl.GroupId = 0) then 1 else if(cm.CommunityId = cl.CommunityId, 1, 0) end 
                        and cm.LocationId = cl.LocationId and cl.ClientId = ?", $id);
                }
                     * 
                     */
        }
        return array();
    }
}